﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;

namespace SARS.Tutor
{
    public partial class AssignNewStudentSession : Page
    {
        private readonly ICourseBO _courseBO = new CourseBO();
        private readonly ISessionBO _sessionBO = new SessionBO();
        private readonly IUserBO _userBO = new UserBO();

        private UserEntity CurrentUser
        {
            get { return Session["CurrentUser"] != null ? (UserEntity) Session["CurrentUser"] : null; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Roles"] == null || RoleType.Coordinator != (RoleType)Session["Roles"])
            {
                Response.Write("<script language='javascript'>window.alert('Your role is not allow to access this page.');" +
                               "window.location='../Account/Login.aspx';</script>");

            }
            if (!IsPostBack)
            {
            }
        }

        protected void ucProgramCourse_OnSelectedCourseChanged()
        {
            IEnumerable<AttendanceSessionEntity> attendanceSessionList = RetrieveAttendanceSession();

            List<StudentEntity> studentList = (RetrieveStudentByCourseId(UcProgramCourse.SelectedCourseValue)
                .Where(x => attendanceSessionList.All(y => y.StudentID != x.UserID))).ToList();
            grvStudents.DataSource = studentList;

            grvStudents.DataBind();

            grvSessions.DataSource = RetrieveSessionByCourseId(UcProgramCourse.SelectedCourseValue);
            grvSessions.DataBind();

            btnSave.Visible = studentList.Count > 0;
        }

        private List<SessionEntity> RetrieveSessionByCourseId(int courseId)
        {
            return _sessionBO.RetrieveSessionByCourseId(courseId);
        }

        //private List<StudentCourseEntity> RetrieveStudentByCourseId(int courseId)
        //{
        //    return _courseBO.RetrieveStudentCourseByCourseId(courseId);
        //}

        private IEnumerable<AttendanceSessionEntity> RetrieveAttendanceSession()
        {
            return _sessionBO.RetrieveAttendanceSessionAll();
        }

        private IEnumerable<StudentEntity> RetrieveStudentByCourseId(int courseId)
        {
            return _userBO.RetrieveStudentByCourseId(courseId);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            List<string> selectedStudents = (from GridViewRow studentRow in grvStudents.Rows
                let cbxStudent = (CheckBox) studentRow.FindControl("ckbStudent")
                where cbxStudent != null && cbxStudent.Checked
                select (HiddenField) studentRow.FindControl("hdnStudentID")
                into hdfStudent
                where hdfStudent != null
                select hdfStudent.Value).ToList();

            List<string> selectedSessions = (from GridViewRow sessionRow in grvSessions.Rows
                let cbxsession = (CheckBox) sessionRow.FindControl("ckbSession")
                where cbxsession != null && cbxsession.Checked
                select (HiddenField) sessionRow.FindControl("hdnSessionID")
                into hdfSession
                where hdfSession != null
                select hdfSession.Value).ToList();

            if (selectedStudents.Count > 0 && selectedSessions.Count > 0)
            {
                List<AttendanceSessionEntity> attendanceSessionList = (from x in selectedSessions
                    from y in selectedStudents
                    select new AttendanceSessionEntity
                    {
                        StudentID = Convert.ToInt32(y),
                        SessionID = Convert.ToInt32(x),
                        IsAbsent = 0,
                        Creator = CurrentUser != null ? CurrentUser.UserID : 1,
                        CreatedDate = DateTime.Now
                    }).ToList();

                if (attendanceSessionList.Count > 0)
                {
                    int result = _sessionBO.InsertAttendanceSession(attendanceSessionList);

                    if (result > 0)
                    {
                        Response.Redirect("StudentAttendance.aspx");
                    }
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Default.aspx");
        }
    }
}